<?php

class UserAdmin extends AdminBase
{
	private $nohf = ['adminadd', 'admindoadd', 'adminrestpwd', 'adminremove', 'updateadmininfo', 'pwddorest'];
	private $mysql = null;

	public function __construct()
	{
		parent::__construct();
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->header('admin');
		}
		$this->mysql = n_api('mysql')->cname('weixin');
		// $this->api = n_api('user');
	}

	public function index()
	{
		$ppid = $_SESSION['ppid'];
		$passport = n_api('passport');
		$data = $passport->get($ppid);
		$this->_html('adminIndex', $data);
	}

	public function updateAdminInfo()
	{
		$param = $this->param("
			nickname:char:ignore,
			birthday:char:ignore,
			gender:char:ignore,
			phone:char:ignore,
			ppid:char:empty
		");

		$ppid = $param['ppid'];
		$param['birthday'] = strtotime($param['birthday']);
		unset($param['ppid']);

		$where = "id = $ppid";
		$this->mysql->update('passport_main', $param, $where);
		echo 'ok';
	}

	public function pwdRest()
	{
		$this->_html('pwdRest');
	}

	public function pwdDoRest()
	{
		$param = $this->param('
			oldpwd:char:empty,
			newpwd:char:empty,
			newpwdconfirm:char:empty
		');

		$ppid = $_SESSION['ppid'];
		$ppInfo = n_api('passport')->get($ppid);

		if ($ppInfo['password'] != $param['oldpwd'])
			exit("密码错误");
		if ($param['newpwd'] != $param['newpwdconfirm'])
			exit("两次输入的密码不匹配");
		if (strlen($param['newpwd'])<6)
			exit("密码长度不能少于6位");

		$newpwd = $param['newpwd'];
		$query = "update passport_main set password = '$newpwd' where id = $ppid";
		$this->mysql->exec($query);
		echo "ok";
	}

	public function userList()
	{
		$query = "select * from passport_main where usertype = 'normal' and isdel ='N' order by regtime desc";
		$userMain = $this->mysql->select($query);
		$ppids = '(';
		foreach ($userMain as $userInfo) {
			$ppids .= "{$userInfo['id']}, ";
		}
		$ppids = trim($ppids, ', ') . ')';
	
		$query = "select max(login_time) as login_time, ppid from passport_log where ppid in $ppids group by ppid";
		$rs = $this->mysql->select($query);
		foreach ($rs as $value) {
			$userLoginLog[$value['ppid']] = $value['login_time'];
		}
		
		$data = [];
		foreach ($userMain as $userInfo) {
			$tmp = $userInfo;
			$tmp['lastLogin'] = $userLoginLog[$userInfo['id']];
			$data[] = $tmp;
		}

		$this->_html('userList', $data);
	}

	public function userDetail()
	{
		$param = $this->param('ppid:uint');
		$ppid = $param['ppid'];

		$unionInfo = n_api('union')->getUnionInfoByPpid($ppid);
		foreach ($unionInfo as $value) {
			$wxids[] = $value['wxid'];
		}
		if (count($wxids) > 0) {
			$rs = n_api('weixin')->getWxDetail($wxids);
			foreach ($rs as $value) {
				$wxDetails[$value['wxid']] = $value;
			}

			$data = [];
			foreach ($unionInfo as $value) {
				$wxid = $value['wxid'];
				$tmp = $wxDetails[$wxid];
				unset($tmp['ctime']);
				$tmp['ctime'] = date('Y-m-d', $value['ctime']);
				$data[] = $tmp;
			}
		} else {
			$data = [];
		}
		//print_r($data);
		$this->_html('userDetail', $data);
	}

	public function adminList()
	{
		if (!n_api('passport')->isSuperAdmin())
			exit('只有超级管理员可以访问此界面');
		$query = "select id, nickname, uname, phone from passport_main where usertype='admin' and isdel='N'";
		$rs = $this->mysql->select($query);
		if (count($rs) == 0) {
			$data['adminInfo'] = [];
			$this->_html('adminList', $data);
			exit;
		}
		foreach ($rs as $value) {
			$ppidArr[] = $value['id'];
		}
		$data['adminInfo'] = $rs;
		$query = "select ppid, max(login_time) as last_login_time from passport_log where ppid in (" . implode(',', $ppidArr) . ") group by ppid";
		$rs = $this->mysql->select($query);
		foreach ($rs as $value) {
			$lastLoginArr[$value['ppid']] = $value['last_login_time'];
		}
		$data['adminLoginTime'] = $lastLoginArr;
		$this->_html('adminList', $data);
	}

	public function adminAdd()
	{
		if (!n_api('passport')->isSuperAdmin())
			exit('只有超级管理员可以访问此界面');
		$this->_html('adminAdd');
	}

	public function adminDoAdd()
	{
		if (!n_api('passport')->isSuperAdmin())
			exit('只有超级管理员可以访问此界面');
		$param = $this->param('name:char:empty,pwd:char:empty');
		$name = $param['name'];
		$pwd = $param['pwd'];

		n_api('passport')->add($name, $pwd, 'admin', '127.0.0.1');
		n_mod("skin")->save("添加成功",3);
	}

	public function adminRestPwd()
	{
		if (!n_api('passport')->isSuperAdmin())
			exit('只有超级管理员可以访问此界面');
		$param = $this->param('ppid:char:empty');
		$ppid = $param['ppid'];

		$query = "update passport_main set password = 123456 where id = $ppid";
		$this->mysql->exec($query);
		n_mod("skin")->save("重置成功！",3);
	}

	public function adminRemove()
	{
		if (!n_api('passport')->isSuperAdmin())
			exit('只有超级管理员可以访问此界面');
		$param = $this->param('ppid:char:empty');
		$ppid = $param['ppid'];

		$this->mysql->del('passport_main', $ppid);
		n_mod("skin")->save("删除成功",3);
	}

	public function __destruct(){
		//加入标准尾。
		if(!in_array(N_ACTION,$this->nohf) && 'page'==N_TYPE){
			n_mod('skin')->footer();
		}
	}
}